package com.ruoyi.record.controller;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.record.domain.RecordSalary;
import com.ruoyi.record.domain.RecordSalaryContent;
import com.ruoyi.record.domain.RecordWage;
import com.ruoyi.record.domain.vo.RecordContent1VO;
import com.ruoyi.record.domain.vo.RecordContent2VO;
import com.ruoyi.record.domain.vo.RecordContent3VO;
import com.ruoyi.record.domain.vo.RecordContent4VO;
import com.ruoyi.record.service.IRecordSalaryContentService;
import com.ruoyi.record.service.IRecordSalaryService;
import com.ruoyi.record.service.IRecordWageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 档案工资Controller
 * 
 * @author ruoyi
 * @date 2023-02-12
 */
@Controller
@RequestMapping("/record/wage")
public class RecordWageController extends BaseController
{
    private String prefix = "record/wage";

    @Autowired
    private IRecordWageService recordWageService;

    @GetMapping()
    public String wage()
    {
        return prefix + "/wage";
    }

    /**
     * 查询档案工资
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecordWage recordWage)
    {
        startPage();
        List<RecordWage> list = recordWageService.selectRecordWageList(recordWage);
        return getDataTable(list);
    }

    @Log(title = "档案工资", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<RecordWage> util = new ExcelUtil<RecordWage>(RecordWage.class);
        List<RecordWage> recordWageList = BeanUtils.copyList(util.importExcel(file.getInputStream()), RecordWage::new);
        String message = recordWageService.importRecordWage(recordWageList, updateSupport, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<RecordWage> util = new ExcelUtil<RecordWage>(RecordWage.class);
        return util.importTemplateExcel("档案工资");
    }

    /**
     * 导出档案工资
     */
    @Log(title = "档案工资", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecordWage recordWage)
    {
        List<RecordWage> list = recordWageService.selectRecordWageList(recordWage);
        ExcelUtil<RecordWage> util = new ExcelUtil<RecordWage>(RecordWage.class);
        return util.exportExcel(list, "档案工资");
    }

    /**
     * 新增档案工资
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存工资详情
     */
    @Log(title = "工资详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecordWage recordWage)
    {
        recordWage.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(recordWageService.insertRecordWage(recordWage));
    }

    /**
     * 修改档案工资
     */
    @GetMapping("/edit/{wageId}")
    public String edit(@PathVariable("wageId") Long wageId, ModelMap mmap)
    {
        RecordWage recordWage = recordWageService.selectRecordWageByWageId(wageId);
        mmap.put("recordWage", recordWage);
        return prefix + "/edit";
    }

    /**
     * 修改保存档案工资
     * @param recordWage
     * @return
     */
    @Log(title = "工资详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecordWage recordWage)
    {
        recordWage.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(recordWageService.updateRecordWage(recordWage));
    }

    /**
     * 删除档案工资
     */
    @Log(title = "档案工资", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recordWageService.deleteRecordWageByWageIds(ids));
    }
}
